﻿using rbac.Domain.Entity;
using rbac.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace rbac.Domain.Impl
{
    public class PermissRepository:IPermissRepository
    {
        private readonly RbacContext _context;

        public PermissRepository(RbacContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 权限显示
        /// </summary>
        /// <returns></returns>
        public IQueryable<Permission> GetPermiss()
        {
            return _context.Permissions;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Add(Permission entity)
        {
            entity.IsDeleted = false;
            _context.Permissions.Add(entity);
            return _context.SaveChanges();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(Permission entity)
        {
            var list = _context.Permissions.Where(c => c.Id == entity.Id).FirstOrDefault();
            list.UpdateTime = DateTime.Now;
            list.PermissionName = entity.PermissionName;
            list.PermissionUrl = entity.PermissionUrl;
            list.Remark= entity.Remark;
            list.ParentId = entity.ParentId;
            return _context.SaveChanges();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int Delete(params int[] ids)
        {
            if(ids.Length<=0)
            {
                return 0;
            }
            var list = _context.Permissions.Where(c => ids.Contains(c.Id));
            foreach(var permiss in list)
            {
                permiss.IsDeleted = true;
            }
            return _context.SaveChanges();

        }

    }
}
